#include <iostream>

#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"


struct State{
    std::string s;
    int pos;
    int step;
    void syncpos_from_s(){
        pos=s.find('.');
    }
    
};

class OneLoop {
public:
    std::unordered_map<std::string,char> reach;
    std::list<State> queue;
    
    void try_up(State& old){
        if(old.pos>2){
            //可以向上
            std::swap(old.s[old.pos],old.s[old.pos-3]);
            if(reach.find(old.s)==reach.end()){
                reach[old.s]=1;
                State news=old;
                news.pos-=3;
                news.step++;
                queue.push_back(news);
            }
            std::swap(old.s[old.pos],old.s[old.pos-3]);
        }
    }
    void try_right(State& old){
        if(old.pos!=2&&old.pos!=5&&old.pos!=8){
            //可以向上
            std::swap(old.s[old.pos],old.s[old.pos+1]);
            if(reach.find(old.s)==reach.end()){
                reach[old.s]=1;
                State news=old;
                news.pos++;
                news.step++;
                queue.push_back(news);
            }
            std::swap(old.s[old.pos],old.s[old.pos+1]);
        }
    }
    void try_bottom(State& old){
        if(old.pos!=6&&old.pos!=7&&old.pos!=8){
            //可以向上
            std::swap(old.s[old.pos],old.s[old.pos+3]);
            if(reach.find(old.s)==reach.end()){
                reach[old.s]=1;
                State news=old;
                news.pos+=3;
                news.step++;
                queue.push_back(news);
            }
            std::swap(old.s[old.pos],old.s[old.pos+3]);
        }
    }
    void try_left(State& old){
        if(old.pos!=0&&old.pos!=3&&old.pos!=6){
            //可以向上
            std::swap(old.s[old.pos],old.s[old.pos-1]);
            if(reach.find(old.s)==reach.end()){
                reach[old.s]=1;
                State news=old;
                news.step++;
                news.pos--;
                queue.push_back(news);
            }
            std::swap(old.s[old.pos],old.s[old.pos-1]);
        }
    }
    
    bool run(
            std::string &s1,std::string &s2
    ) {
        State st;
        st.s=s1;st.step=0;
        st.syncpos_from_s();
        reach[st.s]=1;
        queue.push_back(st);
        while (!queue.empty()){
            auto &out=queue.front();
            if(out.s==s2){
                //到达
                printf("%d\n",out.step);
                break;
            }
            try_bottom(out);
            try_left(out);
            try_right(out);
            try_up(out);
            queue.pop_front();
        }

        return true;
    }
};

class Solution {
public:
    void run() {

        bool first = true;
        int a, b, c, d, e;
        //        std::cin >> a;
        std::string s1,s2;
//        while (1){
        while (std::getline(std::cin,s1)) {
            std::getline(std::cin,s2);
            OneLoop l;
            if (!l.run(s1,s2)) {
//                                return;
            }
        }


    }
};

int main() {

    //    bool cur_num= true;
    Solution().run();
    return 0;
}

